// pages/index/home/home.js
const app = getApp()
const tools = require('../../../utils/tool')

Page({
  data: {
    show: false,
    scrollLeft1: 0,
    actions: [{
        name: '本人拒绝',
      },
      {
        name: '虚假信息',
      },
      {
        name: '盗用信息',
      },
      {
        name: '低俗不当',
      }
    ],
    TabCur: 0,
    TabCur1: 0,
    scrollLeft: 0,
    secondList: [],
    cardCur: 0,
    top: 0,
    TabCurTag: 0,
    hot: [], //热门列表
    recommend: [], //推荐列表
    swiperList: ['https://sch.antaiyi.com/aty%5Cfile%5C%E5%9B%BE%E7%89%87%5C%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220517101738-20220526050956252.png'],
    secondId: 0,
    next_page_url: '', //下一页的url
    url: '/api/applets/article/story', //第一页url
    squareUrl: '/api/applets/article/square', //广场第一页url
    lanmuId: 1, //栏目id
    reportId: 0,
    reportContent: '', //举报的内容
    isShow: false,
    reduceListUrl: '', //记录getReduceList方法当前的url
    subCate: {}, //全部二级菜单列表
    second_cate_id: '', //二级栏目id
    secondClassify:[], //当前显示的二级菜单
    
  },
  onLoad() {
    this.getLanMu()
    this.setData({
      top: app.globalData.CustomBar + 80
    })
    this.setData({
      top: app.globalData.CustomBar + 80
    })
  },
  onShow() {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    let json = currPage.data.mydata;
    if (typeof json == 'undefined') return
    let recommend = this.data.recommend
    let hot = this.data.hot
    console.log(json)
    if (json.isZan) {
      recommend.forEach((item, i) => {
        if (json.id == recommend[i].id) {
          this.setData({
            [`recommend[${i}].praised`]: json.praised
          })
        }
      })
      hot.forEach((item, i) => {
        if (json.id == hot[i].id) {
          this.setData({
            [`hot[${i}].praised`]: json.praised,
            [`hot[${i}].click_num`]: json.ZanCount
          })
        }
      })
    }

    if (json.collect) {
      hot.forEach((item, i) => {
        if (json.id == hot[i].id) {
          this.setData({
            [`hot[${i}].collected`]: json.collected,
            [`hot[${i}].collect_num`]: json.collect_num
          })
        }
      })
    }
 
     
  },
 
  // 获取官⽅栏⽬⾸⻚列表
  getofficialIndex(id) {
    tools.showLoading()
    let official = tools.request('/aty/applet/a/article', 'get', {
      page:0,
      size:10,
      cateId: id
    },wx.getStorageSync('token'))
    official.then(res => {
      console.log(res,1111)
      wx.hideLoading()
      if (res.statusCode==200) {
        this.setData({
          hot:res.data.hot.content,
          recommend:res.data.new.content,
          isShow: true
        })
      }
       console.log(this.data.hot)
    })
  },

  // 获取首页栏目,轮播图
  getLanMu() {
    tools.request('/aty/applet/a/category', 'get', {}).then(res => {
      if (res.statusCode==200) {
       let secondClassify=res.data.subCate[Object.keys(res.data.subCate)[0]]  //取值第一个二级分类
       let id=secondClassify[0].id
       let swiperList=res.data.cates[0].imgs
       let newSwiper=[]
          swiperList.map(item=>{
            newSwiper.push(encodeURI(item.replaceAll('\\', '/')))
        })
        this.setData({
          subCate:res.data.subCate,
          columnList: res.data.cates,
          swiperList:newSwiper,
          secondClassify,
          second_cate_id:id
        })
        console.log(this.data.swiperList)
         this.getofficialIndex(id) 
      } 
    })
  },
  
  // 切换一级栏目
  tabSelect(e) {
    let id = e.currentTarget.dataset.id
     console.log(id)
    let subCate=this.data.subCate
    let index = e.currentTarget.dataset.index
    for (const iterator of this.data.columnList) {
      let swiperList=iterator.imgs
      let newSwiper=[]
      swiperList.forEach(item=>{
        newSwiper.push(encodeURI(item.replaceAll('\\', '/')))
      })
      if (iterator.id == id) {
       
        this.setData({
          swiperList: newSwiper,
          TabCur1: 0
        })
      }
    }
    
     
    // 获取对应二级菜单
    for (const key in subCate) {
      if(id==key){
        if (Object.hasOwnProperty.call(subCate, key)) {
          const element = subCate[key];
        this.setData({
          secondClassify:element,
          second_cate_id:element[0].id
        })
        }
      }
    }
     this.getofficialIndex(this.data.secondClassify[0].id) 
    this.setData({
      TabCur: index,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  // 切换二级分类
  tabSecondClassify(e) {
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    this.setData({
      TabCur1: index,
      second_cate_id: id,
      scrollLeft1: (e.currentTarget.dataset.id - 1) * 60
    })
     this.getofficialIndex(id) 
  },
  showPopup(e) {
    // 点击弹出获取要举报的文章id
    this.setData({
      show: true,
      reportId: e.currentTarget.dataset.id
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  onSelect(event) {
    this.setData({
      show: false,
      reportContent: event.detail.name
    });
    this.getReport()
  },

 
  // 关注
  bindAttention(e) {
    tools.showLoading()
    let author_id = e.currentTarget.dataset.author_id
    let index = e.currentTarget.dataset.index
    let attention = tools.request('/api/applets/follow/add', 'post', {
      user_id: author_id
    }, wx.getStorageSync('token'))
    attention.then(res => {
      wx.hideLoading()
      if (res.data.status) {
        this.setData({
          [`ReduceList[${index}].is_follow`]: !this.data.ReduceList[index].is_follow
        })
        if (this.data.ReduceList[index].is_follow) {
          tools.showToast('关注成功')
        } else {
          tools.showToast('取消成功')
        }
      }
    })
  },

 
  share(e) {
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    tools.request('/api/applets/article/share/' + id, 'get', {}, wx.getStorageSync('token')).then(res => {
      if (res.data.status) {
        this.setData({
          [`ReduceList[${index}].share_num`]: this.data.ReduceList[index].share_num + 1
        })
      }
      if (res.data.error_code === "AUTHORIZATION_INVALID") { // token失效错误代码AUTHORIZATION_INVALID
        this.getToken().then(res => {
          this.share();
        })
      }
    })
  },
  onShareAppMessage(res) {
    let title = res.target.dataset.title
    let imgurl = res.target.dataset.imgurl
    let id = res.target.dataset.id
    return {
      title: title,
      path: '/pages/index/reduce/index?id=' + id,
      imageUrl: imgurl
    }
  },

  // 点赞
  bindGive(e) {
    let id = e.detail.id //文章id
    let index = e.detail.index
    let type = e.currentTarget.dataset.type
    var hot = this.data.hot
    let give = tools.request('/api/applets/article/good/' + id, 'get', {}, wx.getStorageSync('token'))
    give.then(res => {
      if (res.data.status) {
        if (type == 'recommend') {
          this.setData({
            [`recommend[${index}].is_good`]: !this.data.recommend[index].is_good
          })
          if (this.data.recommend[index].is_good) {
            tools.showToast('点赞成功')
            for (var i = 0; i < hot.length; i++) {
              if (id == hot[i].id) {
                this.setData({
                  [`hot[${i}].is_good`]: true,
                  [`hot[${i}].click_num`]: ++this.data.hot[i].click_num
                })
              }
            }
          } else {
            tools.showToast('取消点赞')
            for (var i = 0; i < hot.length; i++) {
              if (id == hot[i].id) {
                this.setData({
                  [`hot[${i}].is_good`]: false,
                  [`hot[${i}].click_num`]: --this.data.hot[i].click_num
                })
              }
            }
          }
        } else {
          // 点赞热门内容同步推荐点赞
          let recommend = this.data.recommend
          if (this.data.hot[index].is_good) {
            this.setData({
              [`hot[${index}].is_good`]: false,
              [`hot[${index}].click_num`]: --this.data.hot[index].click_num
            })
            tools.showToast('取消点赞')
            for (var i = 0; i < recommend.length; i++) {
              if (id == recommend[i].id) {
                this.setData({
                  [`recommend[${i}].is_good`]: false
                })
              }
            }
          } else {
            this.setData({
              [`hot[${index}].is_good`]: true,
              [`hot[${index}].click_num`]: ++this.data.hot[index].click_num
            })
            tools.showToast('点赞成功')
            for (var i = 0; i < recommend.length; i++) {
              if (id == recommend[i].id) {
                this.setData({
                  [`recommend[${i}].is_good`]: true
                })
              }
            }
          }


        }
      }
    })

  }
})